# -----------------------------------------------
# Author: yuz
# Copyright: 湖南零檬信息技术有限公司
# Email: wagyu2016@163.com
# Phone&Wechat: 18173179913
# Site: http://www.lemonban.com
# Forum: http://testingpai.com
# -----------------------------------------------
import base64
import json
import re
import time
import uuid

import jsonpath
import requests
from faker import Faker

from common.db import MySQLHandler
from common.logger import log
from config import Config


def get_img():
    """获取验证码"""
    url = Config.HOST + '/captcha.jpg'
    uid = str(uuid.uuid4())
    resp = requests.get(url, params={"uuid": uid})
    return resp.content, uid


def base64_api(uname, pwd, img, typeid):
    """上传图片"""
    base64_data = base64.b64encode(img)
    b64 = base64_data.decode()
    data = {"username": uname, "password": pwd, "typeid": typeid, "image": b64}
    result = json.loads(requests.post("http://api.ttshitu.com/predict", json=data).text)
    if result['success']:
        return result["data"]["result"]
    else:
        return result["message"]
    return ""


def adminLogin(username, password, code="lemon"):
    """管理员登录.
    访问登录接口封装成函数
    """
    user = {
        "principal": username,
        "credentials": password,
        "imageCode": code
    }
    response = requests.request(
        'POST',
        # url=Config.urls.adminLogin,
        url="http://mall.lemonban.com:8108/adminLogin",
        json=user
    )
    try:
        content = response.json()
        return content['access_token']
    except Exception as e:
        log.error(f"登录失败:{e}")
        raise e


def upload_image(file, token):
    """图片上传"""
    f = open(file, 'rb')
    headers = {"Authorization": f"Bearer{token}"}
    response = requests.request(
        'POST',
        url="http://mall.lemonban.com:8108/admin/file/upload/img",
        files={"file": f},
        headers=headers
    )
    f.close()
    # 2022/06/c46e4c6e808949528628ca5ecad04661.jpg
    return response.text


def send_sms(mobile):
    """发送验证码"""
    data = {"mobile": mobile}
    response = requests.request(
        'PUT',
        # url=Config.urls.adminLogin,
        url="http://mall.lemonban.com:8107/user/sendRegisterSms",
        json=data
    )
    content = response.text
    return content


def check_sms(mobile, validCode):
    """校验验证码"""
    data = {"mobile": mobile, "validCode": validCode}
    response = requests.request(
        'PUT',
        # url=Config.urls.adminLogin,
        url="http://mall.lemonban.com:8107/user/checkRegisterSms",
        json=data
    )
    content = response.text
    print(content)
    if content == 'The verification code is wrong or expired':
        raise ValueError("验证码错误或者超时")
    return content  # flag


def generate_mobile():
    """生成手机号码"""
    fk = Faker(locale=['zh-CN'])
    mobile = fk.phone_number()
    return mobile


def user_login():
    """用户登录"""
    data = {"principal": "yuze", "credentials": "1234", "appType": 3, "loginType": 0}
    response = requests.request(
        'post',
        # url=Config.urls.adminLogin,
        url="http://mall.lemonban.com:8107/login",
        json=data
    )
    return response.json()['access_token']


def get_product(token):
    """用户登录"""
    mydb = MySQLHandler(host='47.113.180.81',
                        port=3306,
                        user='lemon',
                        password='lemon123',
                        database='yami_shops')
    result = mydb.query_one('SELECT prod_id FROM tz_prod where status=1 and total_stocks > 10 and sold_num < 9')
    product_id = result[0]

    sku = mydb.query_one(f'SELECT sku_id FROM tz_sku WHERE prod_id = {product_id} AND is_delete=0 AND stocks > 1')
    sku_id = sku[0]
    # hey
    data = {"addrId": 0,
            "orderItem": {"prodId": product_id, "skuId": sku_id, "prodCount": 1, "shopId": 1},
            "couponIds": [],
            "isScorePay": 0,
            "userChangeCoupon": 0,
            "userUseScore": 0,
            # "uuid":"2ef80031-eb8d-4b3c-8ed8-e33720c4388a"
            }

    headers = {"Authorization": f"bearer{token}"}
    response = requests.request(
        'post',
        # url=Config.urls.adminLogin,
        url="http://mall.lemonban.com:8107/p/order/confirm",
        json=data,
        headers=headers
    )
    return response.text


def submit_order(token):
    """用户登录"""

    data = {"orderShopParam": [{"remarks": "", "shopId": 1}]}

    headers = {"Authorization": f"bearer{token}"}
    response = requests.request(
        'post',
        # url=Config.urls.adminLogin,
        url="http://mall.lemonban.com:8107/p/order/submit",
        json=data,
        headers=headers
    )
    return response.json()['orderNumbers']


def pay_order(token, orderNum):
    """下单"""
    # /p/order/getOrderPayInfoByOrderNumber?orderNumbers=1539147910774788096
    params = {"orderNumbers": orderNum}

    headers = {"Authorization": f"bearer{token}"}
    response = requests.request(
        'get',
        # url=Config.urls.adminLogin,
        url="http://mall.lemonban.com:8107/p/order/getOrderPayInfoByOrderNumber",
        params=params,
        headers=headers
    )
    return response.text


def pay(token, orderId):
    """发起支付"""
    # http://mall.lemonban.com:8107/p/order/pay
    data = {"payType": 3, "orderNumbers": orderId}

    headers = {"Authorization": f"bearer{token}"}
    response = requests.request(
        'post',
        # url=Config.urls.adminLogin,
        url="http://mall.lemonban.com:8107/p/order/pay",
        json=data,
        headers=headers
    )
    return response.text


def callback(token, orderId):
    """发起支付"""
    # http://mall.lemonban.com:8107/notice/pay/3
    data = {"payNo": orderId,
            "bizPayNo": str(int(time.time() * 1000)),
            "isPaySuccess": True, }

    headers = {"Authorization": f"bearer{token}"}
    response = requests.request(
        'post',
        # url=Config.urls.adminLogin,
        url="http://mall.lemonban.com:8107/notice/pay/3",
        json=data,
        headers=headers
    )
    return response.text


class APICase:

    logger = log
    config = Config
    db = MySQLHandler(host='47.113.180.81',
                        port=3306,
                        user='lemon',
                        password='lemon123',
                        database='yami_shops')

    def replace_data(self, my_string):
        """替换 #。。# 标记"""
        result = re.finditer('#(.+?)#', my_string)
        for el in result:
            target = el.group()  # #smsflag#
            prop = el.group(1)  #
            value = getattr(self, prop)
            my_string = my_string.replace(target, value)
        return my_string


    def user_login(self, username, password):
        """用户的登录"""
        data = {"principal":username,"credentials":password,"appType":3,"loginType":0}
        response = requests.request(method='post',
                         json=data,
                         url='http://mall.lemonban.com:8107/login')
        return response.json()['access_token']

    def get_product(self, product_id):
        """获取 product, 2131"""
        url = 'http://mall.lemonban.com:8107/prod/prodInfo'
        response = requests.request(method='get',
                                    url=url,
                                    params={'prodId': product_id})
        content = response.json()
        return product_id, content['skuList'][0]['skuId']


    def get_product_jsonpath(self, product_id):
        """jsonpath 版本"""
        url = 'http://mall.lemonban.com:8107/prod/prodInfo'
        response = requests.request(method='get',
                                    url=url,
                                    params={'prodId': product_id})
        content = response.json()
        sku_id = jsonpath.jsonpath(content, '$..skuId')[0]
        return product_id, sku_id


    def confirm_order(self, token, product_id, sku_id):
        """确认订单"""
        data = {"addrId": 0,
                "orderItem": {"prodId": product_id, "skuId": sku_id, "prodCount": 1, "shopId": 1},
                "couponIds": [],
                "isScorePay": 0,
                "userChangeCoupon": 0,
                "userUseScore": 0,
                # "uuid":"2ef80031-eb8d-4b3c-8ed8-e33720c4388a"
                }

        headers = {"Authorization": f"bearer{token}"}
        response = requests.request(
            method='post',
            url="http://mall.lemonban.com:8107/p/order/confirm",
            json=data,
            headers=headers
        )
        return response.text


    def submit_order(self, token):
        """用户登录, 得到 orderNumber"""

        data = {"orderShopParam": [{"remarks": "", "shopId": 1}]}

        headers = {"Authorization": f"bearer{token}"}
        response = requests.request(
            'post',
            # url=Config.urls.adminLogin,
            url="http://mall.lemonban.com:8107/p/order/submit",
            json=data,
            headers=headers
        )
        return response.json()['orderNumbers']

    def pay(self, token, orderId):
        """发起支付"""
        # http://mall.lemonban.com:8107/p/order/pay
        data = {"payType": 3, "orderNumbers": orderId}

        headers = {"Authorization": f"bearer{token}"}
        response = requests.request(
            'post',
            # url=Config.urls.adminLogin,
            url="http://mall.lemonban.com:8107/p/order/pay",
            json=data,
            headers=headers
        )
        return response.text

    def callback(self, token, orderId):
        """回调"""
        # http://mall.lemonban.com:8107/notice/pay/3
        data = {"payNo": orderId,
                "bizPayNo": str(int(time.time() * 1000)),
                "isPaySuccess": True }

        headers = {"Authorization": f"bearer{token}"}
        response = requests.request(
            'post',
            # url=Config.urls.adminLogin,
            url="http://mall.lemonban.com:8107/notice/pay/3",
            json=data,
            headers=headers
        )
        return response.text


if __name__ == '__main__':

    data = APICase().user_login('yuze', '1234')
    print(data)




